package com.ebs.md.fxmonitor.marketmonitor.alert;

/**
 * Perforce Revision:
 *  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/md/fxmonitor/marketmonitor/alert/AbstractAlertMessageCreatorBySingleFact.java $
 *  $Revision: #4 $  $Change: 115088 $  $DateTime: 2012/09/20 15:49:28 $  $Author: Arik.Sher $
 */

import java.util.List;

import javax.jms.JMSException;
import javax.jms.Message;

import com.ebs.market.monitor.MarketMonitor.EventType;
import com.ebs.market.monitor.MarketMonitor.MarketIdentfier;
import com.ebs.market.monitor.MarketMonitor.OrderDetail;
import com.ebs.md.api.product.PairProduct;
import com.ebs.md.fxmonitor.marketmonitor.CommonTools;
import com.ebs.md.fxmonitor.marketmonitor.model.AbsSingleFact;
import com.ebs.md.fxmonitor.marketmonitor.model.AbsSingleFactByTrader;


public abstract class AbstractAlertMessageCreatorBySingleFact<GPB extends com.google.protobuf.Message, SF extends AbsSingleFact>
extends               AbstractAlertMessageCreator<GPB, SF>
{

	public AbstractAlertMessageCreatorBySingleFact(List<SF> dataList) {
		super(dataList);
	}

	public AbstractAlertMessageCreatorBySingleFact(SF dataItem) {
		super(dataItem);
	}

	protected void doAppendExtraProperties(Message jmsMessage) throws JMSException {
		final SF firstDataElement = getFirstDataElement();
		final PairProduct pair = firstDataElement.getPair();

		jmsMessage.setStringProperty(JMS_MSG_PROPERTY_BASE_CURRENCY_ISO
				, pair.getBaseCcyIso() );
		jmsMessage.setStringProperty(JMS_MSG_PROPERTY_LOCAL_CURRENCY_ISO
				, pair.getLocalCcyIso() );
	}

	protected MarketIdentfier gpbBuildMarketIdentifier(PairProduct pair) {
		MarketIdentfier.Builder miBuilder = MarketIdentfier.newBuilder();
		
		miBuilder.setBaseCurrencyIso (pair.getBaseCcyIso());
		miBuilder.setLocalCurrencyIso(pair.getLocalCcyIso());
		miBuilder.setMatchingType    (pair.getMatchingType());
		
		return miBuilder.build();
	}
	
	protected OrderDetail gpbBuildOrderDetail(AbsSingleFactByTrader asfbt) {
		OrderDetail.Builder odBuilder = OrderDetail.newBuilder();

		appendASF  (odBuilder, asfbt);
		appendASFBT(odBuilder, asfbt);
		
		return odBuilder.build();
	}
	
	protected OrderDetail gpbBuildOrderDetail(AbsSingleFact asf) {
		OrderDetail.Builder odBuilder = OrderDetail.newBuilder();
		
		appendASF(odBuilder, asf);
		
		return odBuilder.build();
	}
	
	private void appendASF(OrderDetail.Builder odBuilder, AbsSingleFact asf) {
		odBuilder.setId(asf.getQuoteId());
		odBuilder.setSubmitTime(asf.getTimestamp());
		odBuilder.setSide(CommonTools.getQuoteSide(asf.getBookSide()));
		odBuilder.setRate(asf.getQuoteRate().toString());
		odBuilder.setAvailAmount(asf.getAvailAmount());
		odBuilder.setTotalAmount(asf.getTotalAmount());
		odBuilder.setRegion(asf.getQuoteRegion());
	}

	private void appendASFBT(OrderDetail.Builder odBuilder, AbsSingleFactByTrader asfbt) {
		odBuilder.setDealCode(asfbt.getFloorCode());
		odBuilder.setTraderId(asfbt.getTraderId());
	}
	
	protected String createEventIdByQuoteId(SF sf, long timestamp) {
		final EventType eventType = getEventType();
		final String truncatedToDate = truncateToDate(timestamp);
		
		String val = 
			String.format("%d#%s#%s"
				,eventType.getNumber()
				,sf.getQuoteId()
				,truncatedToDate
			);
		
		return val;
	}

}
